package com.belf.web.controller.console;

import com.belf.common.annotation.Log;
import com.belf.common.config.ThreadPool;
import com.belf.common.core.controller.BaseController;
import com.belf.common.core.domain.AjaxResult;
import com.belf.common.core.page.TableDataInfo;
import com.belf.common.enums.BusinessType;
import com.belf.common.utils.SecurityUtils;
import com.belf.console.domain.GenerateDataInfo;
import com.belf.console.domain.GenerateDataSql;
import com.belf.console.service.IGenerateDataInfoService;
import com.belf.console.service.IGenerateDataSqlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/console/generateDataInfo")
public class GenerateDataInfoController extends BaseController {


    @Autowired
    private IGenerateDataInfoService dataInfoService;

    @Autowired
    private IGenerateDataSqlService dataSqlService;

    @Autowired
    private ThreadPool threadPool;

    /**
     * 查询生成数据列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestBody GenerateDataInfo generateDataInfo)
    {
        List<GenerateDataInfo> list = dataInfoService.selectGenerateDataInfoList(generateDataInfo);
        long count = dataInfoService.selectDataCount(generateDataInfo);
        return getDataTablePost(list,count);
    }

    /**
     * 获取生成数据信息详细信息
     */
    @PostMapping("/getInfo")
    @ResponseBody
    public AjaxResult getInfo(@RequestBody GenerateDataInfo generateDataInfo)
    {
        return success(dataInfoService.selectGenerateDataInfoById(generateDataInfo.getId()));
    }

    /**
     * 修改生成数据信息
     */
    @Log(title = "生成数据信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(@RequestBody GenerateDataInfo generateDataInfo)
    {
        generateDataInfo.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(dataInfoService.updateGenerateDataInfo(generateDataInfo));
    }

    /**
     * 删除生成数据信息
     */
    @Log(title = "生成数据信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(@RequestBody GenerateDataInfo generateDataInfo)
    {
        return toAjax(dataInfoService.deleteGenerateDataInfoByIds(generateDataInfo.getIds()));
    }

    /**
     * 新增生成数据信息
     */
    @Log(title = "生成数据信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@RequestBody GenerateDataInfo generateDataInfo)
    {
        generateDataInfo.setCreateBy(SecurityUtils.getUsername());
        return toAjax(dataInfoService.insertGenerateDataInfo(generateDataInfo));
    }

    /**
     * 生成源端数据源数据
     * @param generateDataInfo
     * @return
     */
    @PostMapping("/generate")
    @ResponseBody
    public AjaxResult generateSourceData(@RequestBody GenerateDataInfo generateDataInfo)
    {
        threadPool.getExecutorService().execute(() -> {
            dataInfoService.generateSourceData(generateDataInfo);
        });
        return AjaxResult.success();
    }

    /**
     * 修改源端数据源sql
     * @param generateDataSql
     * @return
     */
    @PostMapping("/editDataSql")
    @ResponseBody
    public AjaxResult editDataSql(@RequestBody GenerateDataSql generateDataSql) {
        return toAjax(dataSqlService.updateSql(generateDataSql));
    }

    /**
     * 获取源端数据源sql列表
     * @param generateDataSql
     * @return
     */
    @PostMapping("/listDataSql")
    @ResponseBody
    public AjaxResult listDataSql(@RequestBody GenerateDataSql generateDataSql) {
        return success(dataSqlService.selectDataSqlListByInfoId(generateDataSql));
    }

    /**
     * 创建同步任务
     * @param generateDataInfo
     * @return
     */
    @PostMapping("/createTask")
    @ResponseBody
    public AjaxResult createSyncTask(@RequestBody GenerateDataInfo generateDataInfo)
    {
        return dataInfoService.createSyncTask(generateDataInfo);
    }

}
